package velocity;

import java.io.File;
import java.util.*;

import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;

import java.io.StringWriter;

/**
 * describe
 * author wencong 2022/9/9
 */
public class VelocityMain {

    //    public static String[] tableNames = {"manufacture_bom_child","manufacture_bom_group","manufacture_bom_info","manufacture_replace_child","manufacture_replace_group","manufacture_replace_info","manufacture_replace_main"};
    public static String[] tableNames = {"sales_order","sales_order_wl","sales_outwarehouse","sales_outwarehouse_wl"};
    //    public static String[] vmFileNames = {"Controller.vm","Service.vm","ServiceImpl.vm","Entity.vm", "ApiModelTs.vm","ApiTs.vm","DataTs.vm","IndexVue.vm","ModelVue.vm"};
    public static String[] vmFileNames = {"Controller.vm","Service.vm","ServiceImpl.vm","Entity.vm","DataTs.vm","IndexVue.vm","ModelVue.vm"};
    // 默认存放位置
    public static String depositFilePath = "";//"/Users/wencong/Documents/workspace/intellij-workspace/code-generator/file";

    public static ProjectEntity projectEntity = new ProjectEntity();

    public static void main(String[] args){

        long time = System.currentTimeMillis();
        List<TableEntity> tableEntityList = new ArrayList();
        for(String tableName : tableNames){
            TableEntity tableEntity = MysqlUtils.getTableEntity(tableName);
            Map<String, Object> map = new HashMap<>();
            map.put("tableEntity", tableEntity); // 表属性
            map.put("columnEntityList", MysqlUtils.getColumnEntityList(tableName)); // 列属性
            map.put("projectEntity", projectEntity); // 项目属性
            for (String vmFileName : vmFileNames){
                // 渲染模版后生成字符串内容
                String stringWriter = render(map, vmFileName);
                // 目标文件名
                String targetFile = targetFile(depositFilePath, vmFileName, tableEntity, projectEntity, time);
                System.out.println(targetFile);
                // 生成目标文件
                FileUtil.writer(stringWriter, targetFile);
            }
            tableEntityList.add(tableEntity);
        }
//        String sql1 = "select id from ums_menu order by id desc limit 1";
//        int menuLastId = MysqlUtils.getId(sql1);
//        List<String> modularList = new ArrayList<>();
//        int pid = 0;
//        for(TableEntity tableEntity : tableEntityList){
//            menuLastId = menuLastId + 1;
//            if (!modularList.contains(tableEntity.getModular())){
//                String sql3 = "INSERT INTO `ums_menu`(`id`, `pid`, `name`, `icon`, `type`, `path`, `component`, `sort`, `state`, `ctime`, `admin_id`, `is_show`) VALUES " +
//                        "("+menuLastId+", "+pid+", '"+tableEntity.getTableComment1()+"', 'ion:settings-outline', 1, '/"+tableEntity.getRoute()+"', 'LAYOUT', "+menuLastId+", 1, NULL, NULL, 1);";
//                System.out.println(sql3);
//                String sql5 = "INSERT INTO `ums_role_menu`(`role_id`, `menu_id`) VALUES (1, "+menuLastId+");";
//                System.out.println(sql5);
//                pid = menuLastId;
//                modularList.add(tableEntity.getModular());
//                menuLastId = menuLastId + 1;
//                String sql4 = "INSERT INTO `ums_menu`(`id`, `pid`, `name`, `icon`, `type`, `path`, `component`, `sort`, `state`, `ctime`, `admin_id`, `is_show`) VALUES " +
//                        "("+menuLastId+", "+pid+", '"+tableEntity.getTableComment1()+"', 'ion:settings-outline', 1, '"+tableEntity.getRoute()+"', '/webadmin/"+tableEntity.getModular()+"/"+tableEntity.getRoute()+"/index', "+menuLastId+", 1, NULL, NULL, 1);";
//                System.out.println(sql4);
//                String sql6 = "INSERT INTO `ums_role_menu`(`role_id`, `menu_id`) VALUES (1, "+menuLastId+");";
//                System.out.println(sql6);
//            } else {
//                String sql2 = "select id from ums_menu where `path` = '/"+tableEntity.getModular()+"' and pid = 0 ";
//                int pid1 = MysqlUtils.getId(sql2);
//                pid = pid1 == 0 ? pid : pid1;
//                String sql3 = "INSERT INTO `ums_menu`(`id`, `pid`, `name`, `icon`, `type`, `path`, `component`, `sort`, `state`, `ctime`, `admin_id`, `is_show`) VALUES " +
//                        "("+menuLastId+", "+pid+", '"+tableEntity.getTableComment1()+"', 'ion:settings-outline', 1, '"+tableEntity.getRoute()+"', '/webadmin/"+tableEntity.getModular()+"/"+tableEntity.getRoute()+"/index', "+menuLastId+", 1, NULL, NULL, 1);";
//                System.out.println(sql3);
//                String sql6 = "INSERT INTO `ums_role_menu`(`role_id`, `menu_id`) VALUES (1, "+menuLastId+");";
//                System.out.println(sql6);
//            }
//        }

    }

    public static String targetFile(String depositFilePath, String vmFileName, TableEntity tableEntity, ProjectEntity projectEntity, long time) {
        String path = "";
        boolean bool = !depositFilePath.isBlank();
        String fileName = tableEntity.getTableName1();
        switch (vmFileName){
            case "Controller.vm":
                path = bool ? null : ProjectEntity.netFilePath + "/webadmin/Controllers/" + tableEntity.getModular() + "/";
                fileName = fileName + "Controller.cs";
                break;
            case "Service.vm":
                path = bool ? null : ProjectEntity.netFilePath + "/server/service/" + tableEntity.getModular() + "/";
                fileName = fileName + "Service.cs";
                break;
            case "ServiceImpl.vm":
                path = bool ? null : ProjectEntity.netFilePath + "/server/impl/" + tableEntity.getModular() + "/";
                fileName = fileName + "Impl.cs";
                break;
            case "Entity.vm":
                path = bool ? null : ProjectEntity.netFilePath + "/basics/entity/" + tableEntity.getModular() + "/";
                fileName = fileName + ".cs";
                break;
            case "ApiModelTs.vm":
//                path = "/Users/wencong/Documents/workspace/intellij-workspace/code-generator/file"+ File.separator + projectEntity.getName() + File.separator + time / 1000 + "/";
                path = bool ? null : ProjectEntity.webFilePath + "/src/api/" + tableEntity.getModular() + "/model/";
                fileName = tableEntity.getRoute() + "Model.ts";
                break;
            case "ApiTs.vm":
//                path = "/Users/wencong/Documents/workspace/intellij-workspace/code-generator/file"+ File.separator + projectEntity.getName() + File.separator + time / 1000 + "/";
                path = bool ? null : ProjectEntity.webFilePath + "/src/api/" + tableEntity.getModular() + "/";
                fileName = tableEntity.getRoute() + ".ts";
                break;
            case "DataTs.vm":
                path = bool ? null : ProjectEntity.webFilePath + "/src/views/webadmin/" + tableEntity.getModular() + "/" + tableEntity.getRoute() + "/";
                fileName = tableEntity.getRoute() + ".data.ts";
                break;
            case "IndexVue.vm":
                path = bool ? null : ProjectEntity.webFilePath + "/src/views/webadmin/" + tableEntity.getModular() + "/" + tableEntity.getRoute() + "/";
                fileName = "index.vue";
                break;
            case "ModelVue.vm":
                path = bool ? null : ProjectEntity.webFilePath + "/src/views/webadmin/" + tableEntity.getModular() + "/" + tableEntity.getRoute() + "/";
                fileName = tableEntity.getModular1() + tableEntity.getRoute1() + "Modal.vue";
                break;
        }
        if (bool){
            path = depositFilePath + File.separator + projectEntity.getName() + File.separator + time / 1000 + "/";
        }
        FileUtil.mkdirFile(path);
        return path + fileName;
    }

    public static String render(Map<String, Object> map, String vmFileName) {
        Properties prop = new Properties();
        prop.put("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
        Velocity.init(prop);
        VelocityContext context = new VelocityContext(map);
        StringWriter stringWriter = new StringWriter();
        Template template = Velocity.getTemplate(projectEntity.getName() + File.separator + vmFileName, "UTF-8");
        template.merge(context, stringWriter);
        return stringWriter.toString();
    }

}
